package com.itheima.consumer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

@Slf4j
@Component
public class SpringRabbitListener {
    // 利用RabbitListener来声明要监听的队列信息
    // 将来一旦监听的队列中有了消息，就会推送给当前服务，调用当前方法，处理消息。
    // 可以看到方法体中接收的就是消息体的内容
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage(String msg) throws InterruptedException {
        System.out.println("spring 消费者接收到消息：【" + msg + "】");
        throw new RuntimeException("故意抛出的异常");
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueueMessage(String msg) throws InterruptedException {
        System.out.println("消费者1 接收到消息：【" + msg + "】 , " + LocalTime.now());
        Thread.sleep(25);

    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueueMessage2(String msg) throws InterruptedException {
        System.err.println("消费者2 ..............接收到消息：【" + msg + "】 , " + LocalTime.now());
        Thread.sleep(200);
    }

    // @RabbitListener(queues = "fanout.queue1")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "fanout.queue1", durable = "true"),
            exchange = @Exchange(name = "hmall.fanout", type = ExchangeTypes.FANOUT)
    ))
    public void listenFanoutQueueMessage(String msg) {
        log.info("消费者1 监听到 fanout.queue1 ..............接收到消息：【" + msg + "】");
    }

    // @RabbitListener(queues = "fanout.queue2")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "fanout.queue2", durable = "true"),
            exchange = @Exchange(name = "hmall.fanout", type = ExchangeTypes.FANOUT)
    ))
    public void listenFanoutQueueMessage2(String msg) {
        log.info("消费者2 监听到 fanout.queue2 ..............接收到消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1", durable = "true"),
            exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
    ))
    public void listenDirectQueueMessage(String msg) {
        log.info("消费者1 监听到 direct.queue1 ..............接收到消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2", durable = "true"),
            exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void listenDirectQueueMessage2(String msg) {
        log.info("消费者2 监听到 direct.queue2 ..............接收到消息：【" + msg + "】");
    }

    // @RabbitListener(queues = "topic.queue1")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1", durable = "true"),
            exchange = @Exchange(name = "hmall.topic", type = ExchangeTypes.TOPIC),
            key = {"china.#"}
    ))
    public void listenTopicQueueMessage(String msg) {
        log.info("消费者1 监听到 topic.queue1 ..............接收到消息：【" + msg + "】");
    }

    // @RabbitListener(queues = "topic.queue2")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2", durable = "true"),
            exchange = @Exchange(name = "hmall.topic", type = ExchangeTypes.TOPIC),
            key = {"#.news"}
    ))
    public void listenTopicQueueMessage2(String msg) {
        log.info("消费者2 监听到 topic.queue2 ..............接收到消息：【" + msg + "】");
    }

    @RabbitListener(queues = "object.queue")
    public void listenObjectQueueMessage(Map<String, Object> msg) {
        System.out.println("spring 消费者接收到消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "dlx.queue", durable = "true"),
            exchange = @Exchange(name = "dlx.direct", type = ExchangeTypes.DIRECT),
            key = {"hi"}
    ))
    public void listenDlxExchange(String msg) {
        log.info("消费者 监听到 dlx.queue ..............接收到消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue", durable = "true"),
            exchange = @Exchange(name = "delay.direct", type = ExchangeTypes.DIRECT, delayed = "true"),
            key = {"delay"}
    ))
    public void listenDelayMessassage(String msg) {
        log.info("消费者 监听到 delay.queue ..............接收到消息：【" + msg + "】");
    }


}